package com.wgh.product.controller;

import com.wgh.common.vo.Result;
import com.wgh.common.vo.PageResult;
import com.wgh.product.dto.CategoryDTO;
import com.wgh.product.entity.shop.Category;
import com.wgh.product.service.CategoryService;
import com.wgh.product.converter.CategoryConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 分类控制器
 * 提供分类相关的RESTful API
 */
@RestController
@RequestMapping("/api/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;
    
    @Autowired
    private CategoryConverter categoryConverter;
    
    /**
     * 获取分类树结构
     */
    @GetMapping("/tree")
    public Result<List<CategoryDTO>> getCategoryTree() {
        try {
            List<Category> categories = categoryService.getCategoryTree();
            List<CategoryDTO> categoryDTOs = categoryConverter.toDTOList(categories);
            return Result.success(categoryDTOs);
        } catch (Exception e) {
            return Result.error("获取分类树结构失败：" + e.getMessage());
        }
    }
    
    /**
     * 根据父分类ID查询子分类
     */
    @GetMapping("/sub/{parentId}")
    public Result<List<CategoryDTO>> getSubCategories(@PathVariable Long parentId) {
        try {
            List<Category> categories = categoryService.getSubCategories(parentId);
            List<CategoryDTO> categoryDTOs = categoryConverter.toDTOList(categories);
            return Result.success(categoryDTOs);
        } catch (Exception e) {
            return Result.error("获取子分类失败：" + e.getMessage());
        }
    }
    
    /**
     * 根据分类层级查询分类
     */
    @GetMapping("/level/{level}")
    public Result<List<CategoryDTO>> getCategoriesByLevel(@PathVariable Integer level) {
        try {
            List<Category> categories = categoryService.getCategoriesByLevel(level);
            List<CategoryDTO> categoryDTOs = categoryConverter.toDTOList(categories);
            return Result.success(categoryDTOs);
        } catch (Exception e) {
            return Result.error("获取指定层级分类失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取启用的分类列表
     */
    @GetMapping("/enabled")
    public Result<List<CategoryDTO>> getEnabledCategories() {
        try {
            List<Category> categories = categoryService.getEnabledCategories();
            List<CategoryDTO> categoryDTOs = categoryConverter.toDTOList(categories);
            return Result.success(categoryDTOs);
        } catch (Exception e) {
            return Result.error("获取启用分类列表失败：" + e.getMessage());
        }
    }
    
    /**
     * 查询分类的所有子分类ID，包括自身
     */
    @GetMapping("/{id}/children-ids")
    public Result<List<Long>> getCategoryIdsWithChildren(@PathVariable Long id) {
        try {
            List<Long> categoryIds = categoryService.getCategoryIdsWithChildren(id);
            return Result.success(categoryIds);
        } catch (Exception e) {
            return Result.error("获取分类子ID列表失败：" + e.getMessage());
        }
    }
    
    /**
     * 根据分类名称查询分类
     */
    @GetMapping("/name/{name}")
    public Result<CategoryDTO> getCategoryByName(@PathVariable String name) {
        try {
            Category category = categoryService.getCategoryByName(name);
            if (category == null) {
                return Result.success(null);
            }
            CategoryDTO categoryDTO = categoryConverter.toDTO(category);
            return Result.success(categoryDTO);
        } catch (Exception e) {
            return Result.error("根据名称查询分类失败：" + e.getMessage());
        }
    }
    
    /**
     * 批量更新分类状态
     */
    @PutMapping("/status/batch")
    public Result<Boolean> updateStatusBatch(@RequestParam List<Long> ids, @RequestParam Integer status) {
        try {
            boolean result = categoryService.updateStatusBatch(ids, status);
            return result ? Result.success(true) : Result.error("批量更新分类状态失败");
        } catch (Exception e) {
            return Result.error("批量更新分类状态失败：" + e.getMessage());
        }
    }
    
    /**
     * 新增分类
     */
    @PostMapping
    public Result<Boolean> addCategory(@RequestBody CategoryDTO categoryDTO) {
        try {
            Category category = categoryConverter.toEntity(categoryDTO);
            boolean result = categoryService.addCategory(category);
            return result ? Result.success(true) : Result.error("新增分类失败");
        } catch (Exception e) {
            return Result.error("新增分类失败：" + e.getMessage());
        }
    }
    
    /**
     * 更新分类
     */
    @PutMapping
    public Result<Boolean> updateCategory(@RequestBody CategoryDTO categoryDTO) {
        try {
            Category category = categoryConverter.toEntity(categoryDTO);
            boolean result = categoryService.updateCategory(category);
            return result ? Result.success(true) : Result.error("更新分类失败");
        } catch (Exception e) {
            return Result.error("更新分类失败：" + e.getMessage());
        }
    }
    
    /**
     * 根据ID查询分类详情
     */
    @GetMapping("/{id}")
    public Result<CategoryDTO> getCategoryById(@PathVariable Long id) {
        try {
            Category category = categoryService.getById(id);
            if (category == null) {
                return Result.success(null);
            }
            CategoryDTO categoryDTO = categoryConverter.toDTO(category);
            return Result.success(categoryDTO);
        } catch (Exception e) {
            return Result.error("获取分类详情失败：" + e.getMessage());
        }
    }
    
    /**
     * 根据ID列表批量查询分类
     */
    @GetMapping("/batch")
    public Result<List<CategoryDTO>> getCategoriesByIds(@RequestParam List<Long> ids) {
        try {
            List<Category> categories = categoryService.listByIds(ids);
            List<CategoryDTO> categoryDTOs = categoryConverter.toDTOList(categories);
            return Result.success(categoryDTOs);
        } catch (Exception e) {
            return Result.error("批量查询分类失败：" + e.getMessage());
        }
    }
}